package com.example.fashionshoes.controller.admin;

import com.example.fashionshoes.pojo.po.Coupon;
import com.example.fashionshoes.pojo.po.Result;
import com.example.fashionshoes.pojo.vo.CouponVO;
import com.example.fashionshoes.service.CouponService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCouponController")
@RequestMapping("/coupon")
public class CouponController {
    @Resource
    private CouponService couponService;

    /**
     * 生成优惠劵
     *
     * @return null
     */
    @PostMapping("/add")
    public Result<Object> generateCoupon(@RequestBody Coupon coupon) {
        coupon.setRemaining(coupon.getCount());
        couponService.save(coupon);
        return Result.success();
    }

    /**
     * 获取所有优惠劵信息
     *
     * @return 所有优惠劵信息
     */
    @GetMapping("/all")
    public Result<List<Coupon>> list() {
        List<Coupon> couponList = couponService.list();
        return Result.success(couponList);
    }

    /**
     * 根据id删除优惠劵
     * @param id 优惠劵id
     * @return null
     */
    @DeleteMapping
    public Result<Object> deleteById(Integer id) {
        if (id != null) {
            couponService.removeById(id);
        }
        return Result.success();
    }

    /**
     * 获取要售出的优惠劵
     * @return 售出的优惠劵
     */
    @GetMapping
    public Result<CouponVO> getCoupon(){
        CouponVO couponList = couponService.getCoupon();
        return Result.success(couponList);
    }


}
